package cn.bravolinks.tools.controller;

import cn.bravolinks.tools.constant.Constant;
import cn.bravolinks.tools.domain.User;
import cn.bravolinks.tools.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by yanqin on 2017/6/4.
 */
@Controller
@RequestMapping("login")
public class LoginController extends BaseController{
    @Autowired
    private UserService userService;
    /**
     * 跳转到首页
     * @return
     */
    @RequestMapping("/")
    public String loginPage(){
        return "loginRegisterModal";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(String userid,String password){
        if(!StringUtils.isEmpty(userid) && !StringUtils.isEmpty(password)){
            User u  = userService.getLogin(userid,password);
            if(u!=null) {
                session.setAttribute(Constant.LOGIN_USER,u);
                if (!userid.equals("admin")) {
                    return "success";
                }else{
                    return "adminSuccess";
                }
            }
        }
        return "错误的账号或密码";
    }
}
